﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Shapes;

namespace Gmantis.Controls.Chart
{
    [ContentProperty("Children")]
    public class VisualEffectGroup : VisualEffect
    {
        // Fields
        private System.Collections.ObjectModel.ObservableCollection<VisualEffect> _children;

        // Methods
        private void _children_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (VisualEffect effect in e.NewItems)
                {
                    effect.EffectChanged += new EventHandler(this.effectChanged);
                }
            }
            if (e.OldItems != null)
            {
                foreach (VisualEffect effect2 in e.OldItems)
                {
                    effect2.EffectChanged -= new EventHandler(this.effectChanged);
                }
            }
        }

        internal override UIElement CreateBackgroundEffect(Shape shape)
        {
            Canvas canvas = null;
            UIElement element = null;
            foreach (VisualEffect effect in this.Children)
            {
                UIElement element2 = effect.CreateBackgroundEffect(shape);
                if (element2 != null)
                {
                    if (element == null)
                    {
                        element = element2;
                    }
                    else
                    {
                        if (canvas == null)
                        {
                            canvas = new Canvas
                            {
                                Children = { element }
                            };
                        }
                        canvas.Children.Add(element2);
                    }
                }
            }
            if (canvas != null)
            {
                return canvas;
            }
            return element;
        }

        internal override UIElement CreateForegroundEffect(Shape shape)
        {
            Canvas canvas = null;
            UIElement element = null;
            foreach (VisualEffect effect in this.Children)
            {
                UIElement element2 = effect.CreateForegroundEffect(shape);
                if (element2 != null)
                {
                    if (element == null)
                    {
                        element = element2;
                    }
                    else
                    {
                        if (canvas == null)
                        {
                            canvas = new Canvas
                            {
                                Children = { element }
                            };
                        }
                        canvas.Children.Add(element2);
                    }
                }
            }
            if (canvas != null)
            {
                return canvas;
            }
            return element;
        }

        private void effectChanged(object sender, EventArgs e)
        {
            VisualEffect.OnChanged(this, new DependencyPropertyChangedEventArgs());
        }

        // Properties
        public Collection<VisualEffect> Children
        {
            get
            {
                if (this._children == null)
                {
                    this._children = new System.Collections.ObjectModel.ObservableCollection<VisualEffect>();
                    this._children.CollectionChanged += new NotifyCollectionChangedEventHandler(this._children_CollectionChanged);
                }
                return this._children;
            }
        }
    }
}
